import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/package/api/packageApi.dart';
import 'package:honeypotflutter/modules/package/views/package_list_item.dart';
import 'package:honeypotflutter/public/public_untilController.dart';
import 'package:honeypotflutter/public/views/public_refresh.dart';
import '../../help/package_help.dart';
import '../../model/package_list_model.dart';
import '../../model/package_param.dart';
import 'logic.dart';

class PackageListPage extends StatefulWidget {
  final HLRefreshController? refreshController;
  final PackageType? type;
  final int? deviceId;
  final Function()? recallPackageAction;
  final Function()? modifyPhoneAction;
  final HLUntilController? untilController;
  const PackageListPage({
    Key? key,
    this.refreshController,
    this.untilController,
    this.type,
    this.deviceId,
    this.recallPackageAction,
    this.modifyPhoneAction,
  }) : super(key: key);

  @override
  State<PackageListPage> createState() => _PackageListPageState();
}

class _PackageListPageState extends State<PackageListPage> {
  final PackageListLogic logic = Get.put(PackageListLogic());
  PackageParam param = PackageParam();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.type == PackageType.waitPackage) {
      param.IsTimeout = false;
      param.doorState = 6;
    } else if (widget.type == PackageType.retentionPackage) {
      param.IsTimeout = true;
      param.doorState = 6;
    } else if (widget.type == PackageType.acquiredPackage) {
      param.IsTimeout = false;
      param.doorState = 7;
    }
    if (widget.untilController != null) {
      widget.untilController!.actions = (value) {
        param.keyword = value;
      };
    }
  }

  @override
  Widget build(BuildContext context) {
    return HLRefresh<PackageListItemModel>(
        refreshController: widget.refreshController,
        onRefresh: () {
          param.page = 1;
          return PackageApi.packageList(param);
        },
        onLoading: () {
          param.page += 1;
          return PackageApi.packageList(param);
        },
        snap: (value, int index) {
          if (value.doorState == 6) {
            return PackageListItem(
              itemModel: value,
              type: widget.type ?? PackageType.waitPackage,
              firstButtonTitle: "包裹撤回",
              secondButtonTitle: "短信发送",

              /// 撤回
              firstAction: () {
                logic.recallPackage(
                    orderID: value.id,
                    deviceName: value.deviceName,
                    recallAction: () {
                      if (widget.recallPackageAction != null) {
                        widget.recallPackageAction!();
                      }
                    });
              },

              /// 发送短信
              secondAction: () {
                logic.resend(value.id);
              },

              /// 修改手机号
              modifyPhoneAction: () {
                logic.modifyPhone(
                    model: value,
                    modifySuccess: () {
                      setState(() {});
                      if (widget.modifyPhoneAction != null) {
                        widget.modifyPhoneAction!();
                      }
                    });
              },
            );
          } else {
            return PackageListItem(
              itemModel: value,
              type: PackageType.acquiredPackage,
            );
          }
        });
  }

  @override
  void dispose() {
    Get.delete<PackageListLogic>();
    super.dispose();
  }
}
